<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理</title>
    <link rel="stylesheet" href="bootstrap.css">
    <script src="jQuery.js"></script>
</head>
<body style="padding: 15px;">
    <!-- 添加图书 -->
    
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加图书</h3>
        </div>

        <div class="panel-body form-inline">
            <div class="input-group">
                <div class="input-group-addon">书名</div>
                <input type="text" class="form-control" id="bookname" placeholder="请输入书名">
            </div>
            <div class="input-group">
                <div class="input-group-addon">作者</div>
                <input type="text" class="form-control" id="author" placeholder="请输入作者">
            </div>
            <div class="input-group">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control" id="publisher" placeholder="请输入出版社">
            </div>
                
            <button id="btnAdd" class="btn btn-primary">添加</button>
            
        </div>
    </div>
    

    <!-- 图书列表 -->
    
    <table class="table table-hover table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody class="tb">

        </tbody>
    </table>


<script>
    $(function(){
        function getBookList(){
            $.get('http://www.liulongbin.top:3006/api/getbooks', (res) => {
                if(res.status != 200) return alert('获取数据失败');

                let rows = [];
                console.log(res);       //对象解构 (item -> {})
                $.each(res.data, (i, {id,bookname,author,publisher}) => {
                    rows.push(`<tr>
                                  <td>${id}</td>
                                  <td>${bookname}</td>
                                  <td>${author}</td>
                                  <td>${publisher}</td>
                                  <td><a href="javascript:;" class="del" data-id="${id}">删除</a></td>
                               </tr>`)
                    $('.tb').empty().append(rows.join(''));
                })
            })
        }
        getBookList();

        // 通过事件委托给新添加的.del添加事件
        $('.tb').on('click', '.del', function() {
            let id = $(this).attr('data-id');
            console.log(id);
            $.get('http://www.liulongbin.top:3006/api/delbook', {id: id}, (res) => {
                if(res.status !== 200) return alert('删除失败');
                getBookList();
            })
        })


        $('#btnAdd').on('click', function(){
            let bookname = $('#bookname').val().trim();
            let author = $('#author').val().trim();
            let publisher = $('#publisher').val().trim();

            if(bookname.length <= 0 || author.length <= 0 || publisher.length <= 0){
                return alert('请填写完整的图书信息')
            }

            $.post('http://www.liulongbin.top:3006/api/addbook',
            {bookname: bookname, author: author, publisher: publisher},
            function(res) {
                if(res.staus != 201) return alert(res.msg);
                getBookList();
                $('#bookname').val('');
                $('#author').val('');
                $('#publisher').val('');
            })
        })

    })
</script>

</body>
</html>